package mededelingen.view;

import java.awt.Dimension;

import javax.swing.JFrame;

import prototyping.client.AbstractMessage;
import prototyping.client.teacher.model.TeacherClient;

import javax.swing.*;

import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;

import dao.server.DataLoadIP;
import mededelingen.model.Mededelingenlijst;
import mededelingen.model.MopVanDeDagLijst;
import account.controller.InloggenController;
import account.model.SubTeam;
import account.model.HelpListModel;
import account.model.Jarigen;

import java.awt.SystemColor;

public class TvView extends JFrame {

	private static final long serialVersionUID = 1L;

	private Timer timer;
	private int i = 0;
	private int j = 0;
	private int m = 0; 

	private JLabel lblMededelingen = new JLabel();
	private JTextArea lblMoppen = new JTextArea();
	private JLabel lblJarige = new JLabel("Geen jarige vandaag!");

	private HelpListModel hlm = new HelpListModel();
	private JList<AbstractMessage> list = new JList<AbstractMessage>(hlm);
	
	private String userName;
	private String server;

	public TvView() {
		server = DataLoadIP.getInstance().LoadIP(); //server ip laden
		userName = SubTeam.getInstance().getAcc().getPersoon().getVoornaam() + " " + SubTeam.getInstance().getAcc().getPersoon().getAchternaam(); //voor en achternaam laden
		loadServer(userName, server);
		
		FrameSetup(); //frame laden
		reloadDB(); //database herladen

		//starten van diverse slideshows
		SlideShowMededelingen();
		SlideShowVerjaardagen();
		SlideShowMopVanDedAG();
	}
	
	//docentenclient laden voor server
	private void loadServer(String userName, String server){
		TeacherClient tc = new TeacherClient(server, userName);
		tc.addObserver(hlm);
		tc.start();	
	}
	
	//database elke 60 seconden (tijdelijk) herladen
	private void reloadDB() {
		timer = new Timer(0, reload); 
		timer.start();
		timer.setDelay(60000);
	}

	private ActionListener reload = new ActionListener(){
		public void actionPerformed(ActionEvent ae){   
			//diverse onderdelen worden uit de database geladen 
			Mededelingenlijst.getInstance().loadMededeling();
			Jarigen.getInstance().loadVerjaardag();
			MopVanDeDagLijst.getInstance().loadMop();
			repaint();
		}
	};

	private void FrameSetup() {
		//frame settings
		setUndecorated(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		setMinimumSize(new Dimension(1600, 900));
		setTitle("Mededelingen AO");
		pack();
		
		// Panels
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.WEST);

		JPanel pnlWacht = new JPanel();
		JPanel panel_1 = new JPanel();
		JPanel panel_2 = new JPanel();
		
		// Labels mededelingen
		JLabel lblMededeling = new JLabel("MEDEDELINGEN");
		lblMededeling.setFont(new Font("Tahoma", Font.BOLD, 46));
		JPanel pnlMededeling = new JPanel();
		pnlMededeling.setBorder(null);

		// Labels Mop van de dag
		JLabel lblImage_1 = new JLabel("Mop van de dag");
		lblImage_1.setHorizontalAlignment(SwingConstants.CENTER);
		lblImage_1.setFont(new Font("Tahoma", Font.BOLD, 43));
		lblMoppen.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
		lblMoppen.setBackground(SystemColor.menu);
		
		// Labels van Verjaardagen
		JLabel lblImage = new JLabel("");
		lblImage.setHorizontalAlignment(SwingConstants.CENTER);
		lblImage.setIcon(new ImageIcon(TvView.class.getResource("/tools/images/birthday-ballons-hi - kopie.png")));
		
		JLabel lblVandaagJarig = new JLabel("Vandaag Jarig");
		lblVandaagJarig.setHorizontalAlignment(SwingConstants.CENTER);
		lblVandaagJarig.setFont(new Font("Tahoma", Font.BOLD, 47));
		lblJarige.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblJarige.setHorizontalAlignment(SwingConstants.CENTER);
		
		// Wachtrij
		list.setBackground(SystemColor.menu);
		list.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		list.getAccessibleContext().setAccessibleName("");
		list.setModel(hlm);
		list.setBorder(null);

		// Settings van het scherm
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
				gl_panel_2.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel_2.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
								.addComponent(lblImage_1, GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
								.addComponent(lblMoppen, GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE))
								.addContainerGap())
				);
		gl_panel_2.setVerticalGroup(
				gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblImage_1)
						.addGap(70)
						.addComponent(lblMoppen, GroupLayout.PREFERRED_SIZE, 269, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(135, Short.MAX_VALUE))
				);
		
		panel_2.setLayout(gl_panel_2);	

		//layout van het scherm
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
				gl_panel_1.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel_1.createSequentialGroup()
						.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel_1.createSequentialGroup()
										.addContainerGap()
										.addComponent(lblJarige, GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE))
										.addGroup(gl_panel_1.createSequentialGroup()
												.addContainerGap()
												.addComponent(lblVandaagJarig, GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE))
												.addGroup(Alignment.TRAILING, gl_panel_1.createSequentialGroup()
														.addContainerGap()
														.addComponent(lblImage, GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE)))
														.addContainerGap())
				);
		gl_panel_1.setVerticalGroup(
				gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
						.addComponent(lblVandaagJarig)
						.addGap(18)
						.addComponent(lblJarige, GroupLayout.PREFERRED_SIZE, 198, GroupLayout.PREFERRED_SIZE)
						.addGap(18)
						.addComponent(lblImage, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(146, Short.MAX_VALUE))
				);
		panel_1.setLayout(gl_panel_1);
		
		GroupLayout gl_pnlWacht = new GroupLayout(pnlWacht);
		gl_pnlWacht.setHorizontalGroup(
				gl_pnlWacht.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_pnlWacht.createSequentialGroup()
						.addContainerGap(157, Short.MAX_VALUE)
						.addComponent(list, GroupLayout.PREFERRED_SIZE, 418, GroupLayout.PREFERRED_SIZE)
						.addContainerGap())
				);
		gl_pnlWacht.setVerticalGroup(
				gl_pnlWacht.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pnlWacht.createSequentialGroup()
						.addContainerGap()
						.addComponent(list, GroupLayout.PREFERRED_SIZE, 311, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(47, Short.MAX_VALUE))
				);
		pnlWacht.setLayout(gl_pnlWacht);


		GroupLayout gl_pnlMededeling = new GroupLayout(pnlMededeling);
		gl_pnlMededeling.setHorizontalGroup(
				gl_pnlMededeling.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pnlMededeling.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblMededelingen, GroupLayout.DEFAULT_SIZE, 1604, Short.MAX_VALUE)
						.addContainerGap())
				);
		gl_pnlMededeling.setVerticalGroup(
				gl_pnlMededeling.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pnlMededeling.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblMededelingen, GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
						.addContainerGap())
				);
		lblMededelingen.setHorizontalAlignment(SwingConstants.CENTER);
		gl_pnlMededeling.setAutoCreateContainerGaps(true);
		gl_pnlMededeling.setAutoCreateGaps(true);
		gl_pnlMededeling.setHonorsVisibility(false);
		pnlMededeling.setLayout(gl_pnlMededeling);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
				gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel.createSequentialGroup()
										.addGap(20)
										.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
												.addComponent(pnlMededeling, 0, 0, Short.MAX_VALUE)
												.addGroup(gl_panel.createSequentialGroup()
														.addGap(40)
														.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 391, GroupLayout.PREFERRED_SIZE)
														.addGap(18)
														.addComponent(pnlWacht, GroupLayout.PREFERRED_SIZE, 585, GroupLayout.PREFERRED_SIZE)
														.addGap(18)
														.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 456, GroupLayout.PREFERRED_SIZE))))
														.addGroup(gl_panel.createSequentialGroup()
																.addGap(607)
																.addComponent(lblMededeling)))
																.addContainerGap(122, Short.MAX_VALUE))
				);
		gl_panel.setVerticalGroup(
				gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblMededeling)
						.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(pnlMededeling, GroupLayout.PREFERRED_SIZE, 246, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel.createSequentialGroup()
										.addGap(56)
										.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
												.addComponent(panel_1, 0, 0, Short.MAX_VALUE)
												.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 476, Short.MAX_VALUE)))
												.addGroup(gl_panel.createSequentialGroup()
														.addGap(88)
														.addComponent(pnlWacht, GroupLayout.PREFERRED_SIZE, 369, GroupLayout.PREFERRED_SIZE)))
														.addGap(61))
				);
		panel.setLayout(gl_panel);
	}

	// Slideshow mededelingen
	private void SlideShowMededelingen() {
		timer = new Timer(0, mededelingen); 
		timer.start();
		timer.setDelay(30000);
	}
	private ActionListener mededelingen = new ActionListener(){
		public void actionPerformed(ActionEvent ae){   
			lblMededelingen.setText("<html> <h1> <center>" + Mededelingenlijst.getInstance().getMd().get(i).getNaam() + "</h1> <br/> <h3>"
					+ Mededelingenlijst.getInstance().getMd().get(i).getBeschrijving() + "</h3> </center> </html>");

			i++;
			if (i == Mededelingenlijst.getInstance().getM().size()){
				i = 0;
			}
		}
	};

	// Slideshow verjaardagen
	private void SlideShowVerjaardagen(){
		timer = new Timer(0, jarige);
		timer.start();
		timer.setDelay(60000);
	}
	private ActionListener jarige = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			lblJarige.setText("<html> <h1> <center>" + Jarigen.getInstance().getVd().get(j).getVoornaam() 
					+ " " + Jarigen.getInstance().getVd().get(j).getAchternaam() + "</h1> </center> </html>");

			j++;
			if (j == Jarigen.getInstance().getVd().size()){
				j = 0;
			}
		}
	};

	// Slideshow mop van de dag
	private void SlideShowMopVanDedAG(){ //<body>with<br>linebreak</body>
		timer = new Timer(0, mvdd);
		timer.start();
		timer.setDelay(10000);
	}
	private ActionListener mvdd = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			lblMoppen.setText( MopVanDeDagLijst.getInstance().getMvdd().get(m).getMop());
			m++;

			lblMoppen.setLineWrap(true);
			lblMoppen.setWrapStyleWord(true);
			if ( m == MopVanDeDagLijst.getInstance().getMvdd().size()){
				m = 0;
			}
		}
	};
}